package com.bdqn.vueboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.bdqn.vueboot.config.R;
import com.bdqn.vueboot.dto.CarDto;
import com.bdqn.vueboot.entity.Car;
import com.bdqn.vueboot.service.CarService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/car")
public class CarController {

    @Autowired
    private CarService carService;

    @ApiOperation(value = "查看车辆名单")
    @RequestMapping(value = "/list")
    /* @RequestParam(value = "destination", required = false) String destination,//目的地
                  @RequestParam(value = "proposerName", required = false) String proposerName,//申请人名称
                  @RequestParam(value = "contactsName", required = false) String contactsName,//联系人
                  @RequestParam(value = "departureTime", required = false) Data departureTime,//发车时间
                  @RequestParam(value = "flybackTime", required = false) Data flybackTime//回程时间,*/
    public R list(@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
                  @RequestParam(value = "limit", required = false) Integer limit,
                  @RequestParam(value = "destination", required = false) String destination,//目的地
                  @RequestParam(value = "proposer", required = false) String proposerName,//申请人名称
                  @RequestParam(value = "contactsName", required = false) String contactsName,//联系人
                  @RequestParam(value = "departureTime", required = false) String departureTime,//发车时间
                  @RequestParam(value = "flybackTime", required = false) String flybackTime,//回程时间,
                  @RequestParam(value = "staticId", required = false, defaultValue = "0") Integer staticId//回程时间,
    ) {

        CarDto carData = new CarDto();
        carData.setDestination(destination);
        carData.setProposer(proposerName);
        carData.setContactsName(contactsName);
        carData.setStaticId(staticId);
        if (departureTime != null && departureTime != "") {
            carData.setDepartureTime(Date.valueOf(departureTime));
        }
        if (flybackTime != null && flybackTime != "") {
            carData.setFlybackTime(Date.valueOf(flybackTime));
        }

        IPage<Car> iPage = new Page(pageIndex, limit);
        return R.success(carService.listBiz(iPage, carData));
    }

    @ApiOperation(value = "查看车辆名单")
    @PostMapping(value = "/add")
    public R add(@RequestBody CarDto carDto) {

        int add = carService.add(carDto);

        return R.success(add);
    }

    @ApiOperation(value = "查看车辆名单")
    @GetMapping(value = "/deleteById")
    public R deleteById(@RequestParam(value = "id", required = false) int id) {

        int add = carService.deleteById(id);

        return R.success(add);
    }

    @ApiOperation(value = "查看车辆名单")
    @GetMapping("/audit")
    @CrossOrigin
    public int sevaAudit(@RequestParam(value = "staticId") Integer staticId, @RequestParam(value = "id") Long id) {
        return carService.sevaAudit(staticId, id);
    }

}
